package com.jeesite.modules.my.web;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.Area;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.my.entity.MyRegions;
import com.jeesite.modules.my.service.MyRegionsService;

/**
 * my_regionsController
 * @author ccz
 * @version 2025-03-20
 */
@Controller
@RequestMapping(value = "${adminPath}/my/myRegions")
public class MyRegionsController extends BaseController {

	@Autowired
	private MyRegionsService myRegionsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public MyRegions get(String id, boolean isNewRecord) {
		return myRegionsService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("my:myRegions:view")
	@RequestMapping(value = {"list", ""})
	public String list(MyRegions myRegions, Model model) {
		model.addAttribute("myRegions", myRegions);
		return "modules/my/myRegionsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("my:myRegions:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<MyRegions> listData(MyRegions myRegions, HttpServletRequest request, HttpServletResponse response) {
		myRegions.setPage(new Page<>(request, response));
		Page<MyRegions> page = myRegionsService.findPage(myRegions);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("my:myRegions:view")
	@RequestMapping(value = "form")
	public String form(MyRegions myRegions, Model model) {
		model.addAttribute("myRegions", myRegions);
		return "modules/my/myRegionsForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("my:myRegions:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated MyRegions myRegions) {
		myRegionsService.save(myRegions);
		return renderResult(Global.TRUE, text("保存my_regions成功！"));
	}





	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("my:myRegions:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(MyRegions myRegions) {
		myRegionsService.delete(myRegions);
		return renderResult(Global.TRUE, text("删除my_regions成功！"));
	}








/*
	// 树形结构数据接口
	@RequestMapping("/treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required = false) String parentCode) {
		List<MyRegions> regions = myRegionsService.findByParentId(parentCode);
		System.out.println(regions.toString());
		List<Map<String, Object>> mapList = new ArrayList<>();
		for (MyRegions region : regions) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", region.getId());
			map.put("pId", region.getParentId());
			map.put("name", region.getName());
			map.put("title", region.getName());
			map.put("isParent", true); // 假设所有节点都是父节点
			mapList.add(map);
		}

		return mapList;
	}
*/
@RequiresPermissions("sys:area:view")
@RequestMapping(value = "treeData")
@ResponseBody
public List<Map<String, Object>> treeData(String excludeCode, String parentCode, String isShowCode) {
	List<Map<String, Object>> mapList = ListUtils.newArrayList();
	MyRegions where = new MyRegions();

	// 设置父级编码
	if (StringUtils.isNotBlank(parentCode)) {
		where.setParentId(parentCode);
	}

	List<MyRegions> list = myRegionsService.findList(where);
	Set<String> loadedIds = new HashSet<>(); // 用于记录已经加载的节点ID

	for (int i = 0; i < list.size(); i++) {
		MyRegions e = list.get(i);

		// 过滤被排除的编码（包括所有子级）
		if (StringUtils.isNotBlank(excludeCode)) {
			if (e.getId().equals(excludeCode)) {
				continue;
			}
			if (e.getParentId().contains("," + excludeCode + ",")) {
				continue;
			}
		}

		// 检查是否已经加载过该节点
		if (loadedIds.contains(e.getId())) {
			continue;
		}

		Map<String, Object> map = MapUtils.newHashMap();
		map.put("id", e.getId());
		map.put("pId", e.getParentId());
		map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getParentId(), e.getName()));
		map.put("isParent", true);
		mapList.add(map);

		// 记录已经加载的节点ID
		loadedIds.add(e.getId());
	}
	return mapList;
}
/*	// 分页查询接口
	@GetMapping("/list")
	public List<MyRegions> list() {
		return myRegionsService.findAll();
	}*/
	
}